import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';

import '../constants.dart';

class SpecialRecommendation extends StatelessWidget {
  const SpecialRecommendation();

  @override
  Widget build(BuildContext context) {
    return Container(
      padding: const EdgeInsets.all(12),
      margin: const EdgeInsets.symmetric(horizontal: margin10, vertical: 8),
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.circular(10),
      ),
      child: Column(
        mainAxisSize: MainAxisSize.min,
        children: [
          Row(
            children: [
              CachedNetworkImage(
                imageUrl: urlArtTitle,
                height: 24,
              ),
              Spacer(),
              Text("更多"),
              Icon(
                Icons.arrow_forward_ios,
                size: 14,
              )
            ],
          ),
          grid(recommendedFoodList),
        ],
      ),
    );
  }

  Widget grid(List<Map<String, String>> data) {
    var rows = (data.length / 2).ceil();
    return Column(
      children: [
        ...List.generate(rows, (i) {
          return Row(
            children: [
              _RecommendedFoodItem(data: data[i * 2], isEven: false),
              _RecommendedFoodItem(data: data[i * 2 + 1], isEven: true),
            ],
          );
        }),
      ],
    );
  }
}

class _RecommendedFoodItem extends StatelessWidget {
  final Map<String, String> data;
  final bool isEven;
  _RecommendedFoodItem({Key key, this.data, this.isEven}): super(key: key);

  @override
  Widget build(BuildContext context) {
    return Expanded(
      child: Container(
        margin: EdgeInsets.fromLTRB(isEven ? 6 : 0, 6, isEven ? 0 : 6, 6),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Stack(
              overflow: Overflow.visible,
              alignment: Alignment.bottomRight,
              children: [
                Container(
                  width: double.infinity,
                  height: 100,
                  child: ClipRRect(
                    borderRadius: BorderRadius.circular(6),
                    child: CachedNetworkImage(imageUrl: data['image'], fit: BoxFit.cover),
                  ),
                ),
                Positioned(
                  bottom: -4,
                  right: 4,
                  child: Container(
                    padding: const EdgeInsets.all(1),
                    decoration: BoxDecoration(
                      color: Colors.white,
                      borderRadius: BorderRadius.circular(6),
                    ),
                    width: 30,
                    height: 30,
                    child: ClipRRect(
                      borderRadius: BorderRadius.circular(6),
                      child: CachedNetworkImage(
                        imageUrl: data['thumb'],
                        fit: BoxFit.cover,
                      ),
                    ),
                  ),
                )
              ],
            ),
            Text(
              data['title'],
              style: const TextStyle(
                fontSize: 16,
              ),
            ),
            Text(
              data['desc'],
              style: const TextStyle(color: Color(0xFF806840)),
            ),
          ],
        ),
      ),
    );
  }
}
